/*
 * main.cpp
 *
 *  Created on: 2013-1-13
 *      Author: heqiu
 */

#include <stdio.h>
#include <stdlib.h>
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "myall.h"

const int SCREEN_WIDTH = 600;
const int SCREEN_HEIGHT = 600;
const int SCREEN_BPP = 32;

const int DOT_L = 10;
const int DOT_S = 2;
const float GG2 = 1.4142;

SDL_Surface *g_screen = NULL;
SDL_Event g_event;

void myexit(const char * pFile, int iLen, int iRet, const char * pErr) {
	printf("[%s][%d]err: iRet=%d err=%s\n", pFile, iLen, iRet, pErr);
	exit(0);
}
MapUnit stMapUnit;
void printPoint(uint32_t uiX, uint32_t uiY) {
	MyPoint p(uiX, uiY);
	bool bFlag = stMapUnit.bPointIn(p);

	MyPoint pw;
	stMapUnit.getWinPoint(p, pw);
	{
		printf("win:uiX=%u uiY=%u bool=%d\n", pw.m_x, pw.m_y, bFlag);
		SDL_Rect wall;
		wall.x = (pw.m_x) * GG2 * (DOT_L + DOT_S) / 2;
		wall.y = (pw.m_y) / GG2 * (DOT_L + DOT_S) / 2;
		wall.w = DOT_L* GG2;
		wall.h = DOT_L/ GG2;
		if (bFlag) {
			SDL_FillRect(g_screen, &wall,
					SDL_MapRGB(g_screen->format, 0x44, 0x44, 0xdd));
		} else {
			SDL_FillRect(g_screen, &wall,
					SDL_MapRGB(g_screen->format, 0x00, 0xff, 0x00));
		}
	}

	{
		printf("uiX=%u uiY=%u bool=%d\n", uiX, uiY, bFlag);
		SDL_Rect wall;
		wall.x = uiX * (DOT_L + DOT_S);
		wall.y = uiY * (DOT_L + DOT_S);
		wall.w = DOT_L;
		wall.h = DOT_L;
		if (bFlag) {
			SDL_FillRect(g_screen, &wall,
					SDL_MapRGB(g_screen->format, 0x77, 0x77, 0x77));
		} else {
			SDL_FillRect(g_screen, &wall,
					SDL_MapRGB(g_screen->format, 0xff, 0x00, 0x00));
		}
	}

}

int main(int argc, char ** argv) {
	/*
	 ScaleUnit stScaleUnit;
	 int iRet = stScaleUnit.init(500);
	 if (iRet != 0) {
	 myexit(__FILE__, __LINE__, iRet, "stScaleUnit.init");
	 }
	 MapUnit stMapUnit;
	 iRet = stMapUnit.init(&stScaleUnit, 400, 500);
	 if (iRet != 0) {
	 myexit(__FILE__, __LINE__, iRet, "stMapUnit.init");
	 }

	 MyPoint a, b, c, d;
	 stMapUnit.getPoints(a, b, c, d);
	 a.printOut();
	 b.printOut();
	 c.printOut();
	 d.printOut();
	 printf("[%s][%d]begin...\n", __FILE__, __LINE__);
	 stMapUnit.cycle(printPoint);
	 */
	if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
		myexit(__FILE__, __LINE__, -1, "SDL_Init");
	}
	g_screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,
			SDL_SWSURFACE);
	if (g_screen == NULL) {
		myexit(__FILE__, __LINE__, -1, "SDL_SetVideoMode");
	}
	SDL_WM_SetCaption("project_moving", NULL);

	ScaleUnit stScaleUnit;
	int iRet = stScaleUnit.init(50);
	if (iRet != 0) {
		myexit(__FILE__, __LINE__, iRet, "stScaleUnit.init");
	}

	iRet = stMapUnit.init(&stScaleUnit, 30, 50);
	if (iRet != 0) {
		myexit(__FILE__, __LINE__, iRet, "stMapUnit.init");
	}
	SDL_FillRect(g_screen, &g_screen->clip_rect,
			SDL_MapRGB(g_screen->format, 0xFF, 0xFF, 0xFF));
	stMapUnit.cycle(printPoint);

	/*
	 for (int i = 0; i < 40; i++) {
	 for (int j = 0; j < 40; j++) {
	 printPoint(i, j);
	 }
	 }
	 */

	/*
	 MyPoint a, b, c, d;
	 stMapUnit.getPoints(a, b, c, d);
	 {
	 SDL_Rect wall;
	 wall.x = a.m_x * 20;
	 wall.y = a.m_y * 20;
	 wall.w = 18;
	 wall.h = 18;
	 SDL_FillRect(g_screen, &wall,
	 SDL_MapRGB(g_screen->format, 0x00, 0xff, 0x00));
	 }
	 {
	 SDL_Rect wall;
	 wall.x = b.m_x * 20;
	 wall.y = b.m_y * 20;
	 wall.w = 18;
	 wall.h = 18;
	 SDL_FillRect(g_screen, &wall,
	 SDL_MapRGB(g_screen->format, 0x00, 0xff, 0x00));
	 }
	 {
	 SDL_Rect wall;
	 wall.x = c.m_x * 20;
	 wall.y = c.m_y * 20;
	 wall.w = 18;
	 wall.h = 18;
	 SDL_FillRect(g_screen, &wall,
	 SDL_MapRGB(g_screen->format, 0x00, 0xff, 0x00));
	 }
	 {
	 SDL_Rect wall;
	 wall.x = d.m_x * 20;
	 wall.y = d.m_y * 20;
	 wall.w = 18;
	 wall.h = 18;
	 SDL_FillRect(g_screen, &wall,
	 SDL_MapRGB(g_screen->format, 0x00, 0xff, 0x00));
	 }
	 */
	SDL_Flip(g_screen);

	bool quit = false;
	while (quit == false) {
		while (SDL_PollEvent(&g_event)) {
			if (g_event.type == SDL_QUIT) {
				quit = true;
			}
		}
	}
	SDL_Quit();
	return 0;
}

